import datetime,math
from jili.price.ta import creat_ta_calcor as ta_frame
from copy import deepcopy
from jili.core.printlog import print
#---------------k线字段
#open,high,low,close,volume
#vwap:均价，pos:剩余仓位，updatetime
#使用dict；考虑场景：
def copydict(t):
    r={}
    for k,v in t.items():
        r[k]=v
    return r
class klinenm:
    def __init__(self,obj,period=5,bars_len=2,bar0=None):
        self.obj=obj
        self.period=period
        self.type=type#1:按根数算，2.按时间算
        self.bar={}
        if bar0 is None:
            self.bars=[]
        else:
            self.bars=[bar0]
        self.bars_len=bars_len
        self.onbarcall = []
        self.tas = []
        self.day=None
        self.data_tradetime2=None
        self.ondata = self.onbar
    def add_ta(self, ta_arg):
        ta0 = ta_frame(ta_arg)
        self.tas.append(ta0)

    def __calc_ta(self, bar):
        for i in self.tas:
            r = i.onbar(bar)
            bar.update(r)
        return bar
    def setonbar(self,call):
        self.onbarcall.append(call)
    def getbartimekey(self,timekey):
        if timekey.minute%self.period==0:
            return datetime.datetime(timekey.year,timekey.month,timekey.day,timekey.hour,timekey.minute)
        else:
            r=(int(timekey.minute/self.period)+1)*self.period
            return datetime.datetime(timekey.year,timekey.month,timekey.day,timekey.hour)+datetime.timedelta(minutes=r)
    def isendbar(self,tk0,tk):

        return False
    def onbar(self,t):
        if "obj" in t.keys():
            self.obj=t["obj"]
        timekey0 = t["timekey"]
        timekey=self.getbartimekey(timekey0)
        if self.bars:
            bar0=self.bars[-1]
            if timekey==bar0["timekey"]:
                if bar0["high"]<t["high"]:
                    bar0["high"]=t["high"]
                elif bar0["low"] > t["low"]:
                    bar0["low"] = t["low"]
                bar0["close"]=t["close"]
                bar0["volume"]=bar0["volume"]+t["volume"]
                bar0=self.__calc_ta(bar0)
                self.bar = bar0
            else:
                bar0={}
                bar0["timekey"]=timekey
                bar0["obj"]=self.obj
                bar0["open"]=t["open"]
                bar0["high"]=t["high"]
                bar0["low"]=t["low"]
                bar0["close"]=t["close"]
                bar0["volume"]=t["volume"]
                bar0=self.__calc_ta(bar0)
                self.bars.append(bar0)
                self.bar=bar0
        else:
            bar0={}
            bar0["timekey"]=timekey
            bar0["obj"]=self.obj
            bar0["open"]=t["open"]
            bar0["high"]=t["high"]
            bar0["low"]=t["low"]
            bar0["close"]=t["close"]
            bar0["volume"]=t["volume"]
            bar0=self.__calc_ta(bar0)
            self.bars.append(bar0)
            self.bar=bar0

        if self.isendbar(timekey0, timekey):
            for i in self.onbarcall:
                i(self.bar)
        return self.bar
